<template>
    <div class="mod-prod-prodTag">
        <search-form :showArea="true" :showStatus="true" @search="customSearch"></search-form>
        <avue-crud ref="crud" :page="page" :data="dataList" :table-loading="dataListLoading" :option="tableOption"
                   @selection-change="selectionChange"  @search-change="searchChange" @refresh-change="">

            <template slot-scope="scope" slot="status">
                <el-tag v-if="scope.row.status === 0" size="small" type="danger">禁用</el-tag>
                <el-tag v-else size="small">正常</el-tag>
            </template>

            <template slot-scope="scope" slot="isDfault">
                <el-tag v-if="scope.row.isDefault === 0" size="small">自定义类型</el-tag>
                <el-tag v-else size="small">默认类型</el-tag>
            </template>

            <template slot-scope="scope" slot="menu">
                <el-button type="primary" size="small" icon="el-icon-edit"
                           @click="viewFilling(scope.row)">详情</el-button>
            </template>

        </avue-crud>
        <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @initSuccess="initSuccess" @initFail="initFail"></add-or-update>
        <el-dialog title="数据填报详情" :close-on-click-modal="false" :visible.sync="editVisible" width="95%" height="90vh" top="5vh" custom-class="my-dialog">
            <target-edit ref="editRef" :editFormData="editFormData" controlType="view" @success="success"></target-edit>
        </el-dialog>
    </div>
</template>

<script>
import {
    tableOption
} from '@/crud/analysis/analysis'
import AddOrUpdate from '@/views/modules/filling/main-add-or-update.vue'
import {statusList} from '@/utils/data'
import TargetEdit from '@/template/target-edit/index.vue'
import SearchForm from '@/components/search-form'
export default {
    data() {
        return {
            editFormData: {},
            editVisible: false,
            dataList: [],
            yearsList: [],
            page: {
                total: 0, // 总页数
                currentPage: 1, // 当前页数
                pageSize: 10 // 每页显示多少条
            },
            dataListSelections: [],
            dataListLoading: false,
            tableOption: tableOption,
            permission: {
                delBtn: this.isAuth('prod:prodTag:delete')
            },
            addOrUpdateVisible: false
        }
    },
    components: {
        TargetEdit,
        AddOrUpdate,
        SearchForm
    },
    created() {},
    activated () {
        if(!wfy.empty(wfy.getPara("evalYear"))){
            this.getDataList(1,{
                evalYear: wfy.getPara("evalYear"),
            })
        }
    },
    mounted() {
        this.init();
    },
    methods: {
        init(){
            this.yearsList = [];
            for (let i = 2024; i < 2060; i++) {
                this.yearsList.push({
                    value : i,
                    label : i
                })
            }
            let areaList = this.$store.state.user.area.area;
            this.tableOption.column.forEach((item) => {
                if(item.prop === 'evalYear'){
                    item.dicData = this.yearsList
                }
                if(item.prop === 'evalStatus'){
                    item.dicData = statusList
                }
                if(item.prop === "reginId"){
                    areaList.forEach((area) => {
                        item.dicData.push({
                            value : area.code,
                            label : area.name
                        })
                    })
                    console.log('🚀 ~ item.dicData', item.dicData)
                }
            })
        },
        customSearch(params){
            this.getDataList(1,{
                evalYear: params.evalYear,
                evalStatus: params.evalStatus,
                reginId: params.regionId,
                provinceId: params.provinceId,
                cityId: params.cityId
            })
        },
        getDataList(page, params, done) {
            console.log('🚀 ~ page, params', page, params)
            this.dataListLoading = true
            this.dataList = []
            this.$http({
                url: this.$http.adornUrl('/eval/getEvalInfoList'),
                method: 'post',
                data: this.$http.adornData(
                    Object.assign(
                        {
                            current: page == null ? this.page.currentPage : page.currentPage,
                            size: page == null ? this.page.pageSize : page.pageSize
                        },
                        params
                    )
                )
            }).then(({
                data
            }) => {
                console.log('🚀 ~ data', data)
                this.dataList = data;
                // this.totalResult.forEach((item) => {
                //     this.dataList.push(item.evaluationMain)
                // })
                this.page.total = data.length
                this.dataListLoading = false
                if (done) {
                    done()
                }
            })
        },
        // 新增 / 修改
        addOrUpdateHandle(id) {
            this.addOrUpdateVisible = true
            this.$nextTick(() => {
                this.$refs.addOrUpdate.init(id)
            })
        },
        // 修改
        viewFilling(row){
            console.log('🚀 ~ row', row)
            this.$http({
                url: this.$http.adornUrl('/eval/getNowEvalInfo'),
                method: "post",
                data: this.$http.adornData({
                    "evalMainId": row.evalMainId,
                    "evalYear": row.evalYear,
                    "reginId": row.reginId
                })
            }).then(({
                data
            }) => {
                console.log('🚀 ~ data', data)
                let orReadyItem = {
                    affixList: data[0].affixList,
                    anIndexEvaluationMain: data[0].evaluationMain,
                    currentEvalDtl: data[0].currentEvalDtl,
                    currentIndexInfo: data[0].currentIndexInfo,
                    scoreList: data[0].scoreList
                }
                this.initSuccess(orReadyItem);
            })

        },
        handleConfirm(){
            this.auditFunction(this.dataListSelections);
        },
        auditFunction(checkedArr){
            let promiseArr = [];
            checkedArr.forEach((item) => {
                promiseArr.push(this.updMainSelCol(item));
            })
            Promise.all(promiseArr).then(allRes => {
                this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500,
                    onClose: () => {
                        this.getDataList(this.page)
                    }
                })
            })
        },
        // 主信息操作
        updMainSelCol(evalMain){
            return new Promise((resolve) => {
                this.$http({
                    url: this.$http.adornUrl('/eval/updMainSelCol'),
                    method: "post",
                    data: this.$http.adornData({
                        "evalMainId": evalMain.evalMainId,
                        "evalStatus": "C"
                    })
                }).then(({
                    data
                }) => {
                    resolve(true);
                })
            })
        },
        // 多选变化
        selectionChange (val) {
            this.dataListSelections = val
        },
        // 获取选中的商品Id列表
        getSeleProdIds () {
            return this.dataListSelections.map(item => {
                return item.indexId
            })
        },
        /**
         * 刷新回调
         */
        refreshChange() {
            this.getDataList(this.page)
        },
        initSuccess(data) {
            console.log('🚀 ~ view-------data', data)
            // sessionStorage.viewInitFormData = JSON.stringify(wfy.deepCopy(data));
            // this.$router.push({
            //     path: 'analysis-detail',
            //     query: { id : data.anIndexEvaluationMain.evalMainId }
            // })

            this.editFormData = data;
            this.$nextTick(() => {
                this.editVisible = true;
                this.$refs.editRef.initData();
            })
        },
        // 新增失败  -- 一般是指标没有对应
        initFail(){

        },
        searchChange(params, done) {
            console.log('🚀 ~ params', params)
            if(wfy.empty(params)){
                this.$message.warning("查询条件不能为空");
                done()
                return;
            }
            /*****
            if(wfy.empty(params.reginId)){
                this.$message.warning("填报区县不能为空");
                done()
                return;
            }
            if(wfy.empty(params.evalYear)){
                this.$message.warning("填报年度不能为空");
                done()
                return;
            }
            if(wfy.empty(params.evalStatus)){
                this.$message.warning("请选择状态");
                done()
                return;
            }*/
            this.getDataList(this.page, params, done)
        },
        success(message){
            console.info(message)
            // this.removeTabHandle(this.mainTabsActiveName)
            // this.$router.push({
            //     path: 'analysis-list',
            //     query: {  evalYear : message.evalYear }
            // })
            this.editVisible = false;
            this.getDataList(1,{
                evalYear: message.evalYear
            })
        }
    }
}
</script>

<style scoped>

.mod-prod-prodTag >>> .my-dialog{
    height: 90vh !important;
    overflow-y: scroll;
}

</style>
